package com.heatingcharge.service.pay.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heatingcharge.common.util.ChargeModeEnum;
import com.heatingcharge.common.util.StringUtil;
import com.heatingcharge.entity.base.HeatYear;
import com.heatingcharge.entity.charge.ChargePayable;
import com.heatingcharge.entity.pay.AliPayBean;
import com.heatingcharge.entity.pay.PayInfo;
import com.heatingcharge.mapper.charge.ChargePayableMapper;
import com.heatingcharge.param.ChargeInfoParam;
import com.heatingcharge.security.JwtUser;
import com.heatingcharge.security.util.SecurityUtil;
import com.heatingcharge.service.base.IHeatYearService;
import com.heatingcharge.service.charge.IChargeInfoService;
import com.heatingcharge.service.pay.IAliPayService;
import com.heatingcharge.service.pay.IPayInfoService;
import com.ijpay.alipay.AliPayApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

import java.util.Date;
import java.util.Map;

import static com.ijpay.alipay.AliPayApi.pageExecute;

/**
 * Created with IntelliJ IDEA.
 * Description: heating_charge_server
 * User: Devin
 * Date: 2019-12-13
 * Time: 14:56
 */
@Service
@Slf4j
public class AliPayServiceImpl implements IAliPayService {
    @Autowired
    ChargePayableMapper chargePayableMapper;
    @Autowired
    AliPayBean aliPayBean;
    @Autowired
    IHeatYearService heatYearService;
    @Autowired
    IPayInfoService payInfoService;


    private final static String NOTIFY_URL = "aliPay/notify_url";
    private final static String RETURN_URL = "aliPay/return_url";

    @Override
    public void pcPay(ChargeInfoParam chargeInfoParam,
                      HttpServletResponse response) {
        QueryWrapper<ChargePayable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", chargeInfoParam.getChargePayableId());
        queryWrapper.eq("del_flag", 0);
        ChargePayable chargePayable = chargePayableMapper.selectOne(queryWrapper);
        HeatYear heatYear = heatYearService.getById(chargePayable.getHeatYearId());
        try {
//            String outTradeNo = chargePayable.getHeatYearId() + "_" + chargePayable.getHouseId() + "_" + StringUtil.getOutTradeNo();
            String totalAmount = chargeInfoParam.getActual() + "";
            String returnUrl = aliPayBean.getDomain() + RETURN_URL;
            String notifyUrl = aliPayBean.getDomain() + NOTIFY_URL;
            AlipayTradePagePayModel model = new AlipayTradePagePayModel();
            model.setOutTradeNo(chargeInfoParam.getOrderNo());
            model.setProductCode("FAST_INSTANT_TRADE_PAY");
            model.setTotalAmount(totalAmount);
            model.setSubject("供暖费");
            model.setBody(heatYear.getYearName() + "供暖费");
            model.setPassbackParams("passback_params");
            AliPayApi.tradePage(response, model, notifyUrl, returnUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String payTest(HttpServletResponse response) {
        try {
            String totalAmount = "0.88";
            String outTradeNo = StringUtil.getOutTradeNo();

            String returnUrl = aliPayBean.getDomain() + RETURN_URL;
            String notifyUrl = aliPayBean.getDomain() + NOTIFY_URL;
            AlipayTradePagePayModel model = new AlipayTradePagePayModel();
            model.setOutTradeNo(outTradeNo);
            model.setProductCode("FAST_INSTANT_TRADE_PAY");
            model.setTotalAmount(totalAmount);
            model.setSubject("Javen PC支付测试");
            model.setBody("Javen IJPay PC支付测试");
            model.setPassbackParams("passback_params");
            log.info("notifyUrl:" + notifyUrl);
            log.info("returnUrl:" + returnUrl);
            // AliPayApi.tradePage(response, model, notifyUrl, returnUrl);
            //AlipayTradePrecreateResponse response1 = AliPayApi.tradePrecreatePayToResponse(model1, notifyUrl);
            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            request.setBizModel(model);
            request.setNotifyUrl(notifyUrl);
            request.setReturnUrl(returnUrl);
            String form = ((AlipayTradePagePayResponse) pageExecute(request)).getBody();
            return form;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
